2.ExoPlayer MediaSource简介
在ExoPlayer里每一种媒体资源都是被MediaSource来代表的。
/**
* Defines and provides media to be played by an {@link com.google.android.exoplayer2.ExoPlayer}. A
* MediaSource has two main responsibilities:
*
* <ul>
* <li>To provide the player with a {@link Timeline} defining the structure of its media, and to
* provide a new timeline whenever the structure of the media changes. The MediaSource
* provides these timelines by calling {@link SourceInfoRefreshListener#onSourceInfoRefreshed}
* on the {@link SourceInfoRefreshListener}s passed to {@link
* #prepareSource(SourceInfoRefreshListener, TransferListener)}.
* <li>To provide {@link MediaPeriod} instances for the periods in its timeline. MediaPeriods are
* obtained by calling {@link #createPeriod(MediaPeriodId, Allocator, long)}, and provide a
* way for the player to load and read the media.
* </ul>
*
* All methods are called on the player's internal playback thread, as described in the {@link
* com.google.android.exoplayer2.ExoPlayer} Javadoc. They should not be called directly from
* application code. Instances can be re-used, but only for one {@link
* com.google.android.exoplayer2.ExoPlayer} instance simultaneously.
*/
public interface MediaSource {
....
}
ExoPlayer中MediaSource接口有很多实现类:
- ProgressiveMediaSource: 常规资源
- SsMediaSource: SmoothStreaming资源
- HlsMediaSource: HLS资源
- DashMediaSource: DASH资源
- LoopingMediaSource:循环播放
- MergingMediaSource:合并
- ClippingMediaSource:裁剪
- ConcatenatingMediaSource:列表
ConcatenatingMediaSource
连接资源的转换是无缝的。这种连接不要求是相同格式的资源(例如可以把包含480P H264视频文件和包含720P VP9的视频文件很好地连接在一起)。它们甚至可以是不同的类型(比如可以将一个视频和一个纯音频流很好地连接在一起)。并且在一个连接里一个类型的MediaSource可以被多次使用。
在一个ConcatenatingMediaSource里可以通过添加,删除和移动MediaSource动态地修改播放列表。同样在播放视频之前或者是正在播放的过程中可以通过调用相应的ConcatenatingMediaSource方法动态修改播放列表。播放器会正确地自动处理这些动态修改。例如正在播放的MediaSource被移动了,播放不会中断并且播放完成后会自动播放它后面的一个MediaSource资源。如果正在播放的MediaSource被删除了,播放器会自动移动到第一个存在的后继者去播放,如果没有后继者的话,播放器将会转到结束的状态。
val mediaSource = buildMediaSource(uri)
val mediaSource2 = buildMediaSource(uri)
val concatenatingMediaSource = ConcatenatingMediaSource(mediaSource, mediaSource2)
mPlayer.prepare(concatenatingMediaSource)
private fun buildMediaSource(uri: Uri): MediaSource {
val dataSourceFactory = DefaultDataSourceFactory(context, Util.getUserAgent(context, "you application name"))
return ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri)
}
ClippingMediaSource
剪辑视频
// 从5s到10s
val clippingMediaSource = ClippingMediaSource(mediaSource, 5000, 10000)
mPlayer.prepare(clippingMediaSource)
LoopingMediaSource
循环播放,如果想要一直循环,可以使用ExoPlayer.setRepeatMode.
// 循环5次
val loopingMediaSource = LoopingMediaSource(mediaSource, 5)
mPlayer.prepare(loopingMediaSource)
MergingMediaSource
如果一个视频文件和一个单独的字母文件,可以使用MergingMediaSource合并成一个资源播放
高级组合
假如有两个视频A和B,下面的例子展示了怎么一起使用LoopingMediaSource和ConcatenatingMediaSource来播放A、A、B序列。
val mediaSource = buildMediaSource(uri)
val mediaSource2 = buildMediaSource(uri)
val loopingMediaSource = LoopingMediaSource(mediaSource, 2)
val concatenatingMediaSource = ConcatenatingMediaSource(loopingMediaSource, mediaSource2)
mPlayer.prepare(concatenatingMediaSource)
ExoPlayer提供下载媒体以进行离线播放的功能。这里就不细说了。
上一篇: 1. ExoPlayer简介
下一篇: 3. ExoPlayer源码分析之prepare方法
- 邮箱 :[email protected]
- Good Luck!